// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
// run()是所有angular应用中第1个被执行的方法
.run(function($ionicPlatform) {
  // $ionicPlatform可以检测当前平台
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})
// $stateProvider配置路由状态
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {

    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('standard');

    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
    $ionicConfigProvider.platform.android.navBar.alignTitle('center');

    $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
    $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

    $ionicConfigProvider.platform.ios.views.transition('ios');
    $ionicConfigProvider.platform.android.views.transition('android');

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '/tab',
    abstract: true,
    // 模板的url
    // www目录下templates是模板目录
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

  .state('tab.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/tab-home.html',
        controller: 'homeCtrl'
      },
      'user@tab.home':{
        templateUrl: 'templates/user.html'
      }
    }
  })

  .state('tab.discover', {
      url: '/discover',
      views: {
        'tab-discover': {
          templateUrl: 'templates/tab-discover.html',
          controller: 'discoverCtrl'
        },
        'dynamic@tab.discover':{
          templateUrl: 'templates/dynamic.html'
        }
      }
    })
    .state('tab.banner-content', {
      url: '/discover/banner-content',
      views: {
        'tab-discover': {
          templateUrl: 'templates/banner-content.html',
          controller: ''
        },
        'user@tab.banner-content':{
          templateUrl: 'templates/user.html'
        },
        'article@tab.banner-content':{
          templateUrl: 'templates/article.html'
        },
        'comment@tab.banner-content':{
          templateUrl:'templates/comment.html'
        }
      }
    })
    //精选动态详情
    .state('tab.chat-detail', {
      url: '/discover/chat-detail/:id',
      views: {
        'tab-discover': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'discoverCtrl'
        },
        'user@tab.chat-detail':{
          templateUrl: 'templates/user.html'
        },
        'more@tab.chat-detail':{
          templateUrl: 'templates/more.html'
        },
        'comment@tab.chat-detail':{
          templateUrl:'templates/comment.html'
        }
      }
    })
    //精选话题列表
    .state('tab.topic-list', {
      url: '/discover/topic-list',
      views: {
        'tab-discover': {
          templateUrl: 'templates/selectedtopic.html',
          controller: 'chatDetailCtrl'
        }
      }
    })
  //  精选话题详情
    .state('tab.topic-detail', {
      url: '/discover/topic-detail/:chatId',
      views: {
        'tab-discover': {
          templateUrl: 'templates/content.html',
          controller: ''
        }
      }
    })
    //精选合集列表
    .state('tab.keeper-list', {
      url: '/discover/keeper-list',
      views: {
        'tab-discover': {
          templateUrl: 'templates/keepers.html',
          controller: ''
        }
      }
    })
    //  精选合集详情
    .state('tab.keepers-detail', {
      url: '/discover/keepers-detail',
      views: {
        'tab-discover': {
          templateUrl: 'templates/keeperscontent.html',
          controller: ''
        },
        'dynamic@tab.keepers-detail':{
          templateUrl: 'templates/dynamic.html',
          controller: 'discoverCtrl'
        }
      }
    })

  .state('tab.course', {
    url: '/course',
    views: {
      'tab-course': {
        templateUrl: 'templates/tab-course.html',
        controller: 'courseCtrl'
      }
    }
  })
    .state('tab.reg', {
      url: '/reg',
      views: {
        'tab-me': {
          templateUrl: 'templates/reg.html',
          controller: 'regCtrl'
        }
      }
    })
    .state('tab.me', {
      url: '/me',
      views: {
        'tab-me': {
          templateUrl: 'templates/tab-me.html',
          controller: 'meCtrl'
        }
      }
    })
    //登录
    .state('tab.login', {
      url: '/login',
      views: {
        'tab-me': {
          templateUrl: 'templates/login.html',
          controller: 'loginCtrl'
        }
      }
    });
  // $urlRouterProvider设置默认路由
  // 负责处理在状态配置中指定的url路由方式之外的url请求的路由方式
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/home');

})

.controller('headerCtrl',function($scope){
    $scope.show = function(){
      $('div.search').removeClass('hide')
    }
  })
.controller('tabCtrl',function($scope){
  $scope.userUrl = ($.cookie('user')) ? '#/tab/me' : '#/tab/login';

})
